package com.mfs.common.holder;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 线程持有者
 * @author mfs
 * @date 2022/07/12
 * */
public class ThreadHolder {
    // name -> thread
    private static Map<String, Thread> nameToThreadMap = new ConcurrentHashMap<>();

    public static Map<String, Thread> getAllThread() {
        return new HashMap<>(nameToThreadMap);
    }

    public static boolean add(Thread thread) {
        nameToThreadMap.put(thread.getName(), thread);
        return true;
    }

    public static boolean remove(Thread thread) {
        return remove(thread.getName());
    }

    public static boolean remove(String name) {
        nameToThreadMap.remove(name);
        return true;
    }

    public static Thread get(String name) {
        return nameToThreadMap.get(name);
    }
}
